<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
<%  
    String path="ws://"+request.getServerName()+":8981"+request.getContextPath()+"/";  
%>  
<!DOCTYPE html>  
<html>  
<head>  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    <title>websocketIndex</title>  
    <script type="text/javascript">  
        var wsuri = "<%=path%>socket.html";  
        var ws = null;  
        function startWebSocket() {  
            if ('WebSocket' in window)  //判断浏览器类型
                ws = new WebSocket(wsuri);  
            else if ('MozWebSocket' in window)  
                ws = new MozWebSocket(wsuri);  
            else  
                console.error("not support WebSocket!");  
       		     ws.onmessage = function(evt) { 
       		   		console.log(evt.data);
         		 };  
  
            ws.onclose = function(evt) {  
                alert("close");  
                console.info(evt);  
            };  
  
            ws.onopen = function(evt) {  
                alert("open");  
                console.info(evt);  
            };  
        };  
          
        function init(){  
            startWebSocket();  
        };  
      
        function sendMsg(){           
            ws.send(document.getElementById('writeMsg').value);  
        }  
        function closeSocket(){    
        	alert("closeSocket");
        	ws.onclose;
        }  
    </script>  
</head>  
<body>  
<input type="text" id="writeMsg"/>   
<input type="button" value="sendSmgToServer" onclick="sendMsg()"/>  
<input type="button" value="open" onclick="init()"/>  
<input type="button" value="close" onclick="closeSocket()"/>  
<br>  
<span>  
	每3秒收到更新。
</span>  
</body>  
</html>  